import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef, ViewEncapsulation } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import { MaterialIntoServiceProxy, LocationServiceProxy, LocationOfMaterialListDto, AppLicationListDto, AppLicationTreeDto, SignMaterialForProductInput, GetMaterialForProductForSignOutPut, MaterialForProductSignDto, OutStockBillListDto, InboundInventoryOfMaterialInput, IntoWarehouseDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'putIntoModal',
  templateUrl: './put-into-modal.component.html',
  styleUrls: ['./put-into-modal.component.less'],
  encapsulation: ViewEncapsulation.None,
})

export class PutIntoModalComponent extends AppComponentBase {

  @ViewChild('actionModal') modal: ModalDirective;

  @ViewChild('areaCombobox') areaCombobox: ElementRef;
  @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
  @ViewChild('positionCombobox') positionCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  activeIndex = 0;

  positionId = "";
  // areas = new Array<AppLicationTreeDto>();
  // area = new AppLicationTreeDto();
  bills = new GetMaterialForProductForSignOutPut();
  hasLoadLocations = false;

  items = [
    { label: '数据校验' },
    { label: '位置选择' }
  ];

  constructor(
    injector: Injector,
    private _warehouseIntoServiceProxy: MaterialIntoServiceProxy,
    private _locationServiceProxy: LocationServiceProxy,

  ) {
    super(injector);
  }

  show(hgzNo: string): void {
    this.activeIndex = 0;
    this.active = true;
    this.hasLoadLocations = false;
    var hgz = new SignMaterialForProductInput();
    this.bills.outStockBill = new OutStockBillListDto();
    this.bills.materialForProductSign = new MaterialForProductSignDto();
    hgz.certificationNo = hgzNo;
    this._warehouseIntoServiceProxy.scanMaterialByHgz(hgz)
      .subscribe(result => {
        this.bills = result;
        this.modal.show();
      });
  }

  onNextBtnClick() {
    this.activeIndex = 1;

    if (!this.hasLoadLocations) {
      // this._locationServiceProxy.getAreas("", true, 51)
      //   .subscribe(result => {
      this.hasLoadLocations = true;
      //   this.areas = result.items;
      //   // setTimeout(() => {
      //   //   $(this.areaCombobox.nativeElement).selectpicker('refresh');
      //   // }, 0);
      // });
    }
  }



  save(): void {
    this.saving = true;

    let input = new IntoWarehouseDto();
    input.locationOfMaterialId = this.positionId;
    input.outStockBillId = this.bills.outStockBill.id;
    this.bills.materialForProductSign.departmentId = 58;
    input.materialForProductSignDto = this.bills.materialForProductSign;


    this._warehouseIntoServiceProxy.intoWarehouse(input)
      .pipe(finalize(() => {
        this.saving = false;
      }))
      .subscribe((result) => {
        this.notify.info('入库成功');
        this.close();
        this.modalSave.emit();
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
